package cn.itcast.tanhua.mytanhuaserver.controller;

import cn.itcast.tanhua.mytanhuaserver.service.TestSoulService;
import cn.itcast.tanhua.pojo.Answers;
import cn.itcast.tanhua.vo.QuestionnaireVo;
import cn.itcast.tanhua.vo.Questions;
import cn.itcast.tanhua.vo.UserReportVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: 杨雅雯
 * @Date: 2021/12/8 12:12
 */
@RequestMapping("testSoul")
@RestController
public class TestSoulController {

    @Autowired
    private TestSoulService testSoulService;

    /**
     * 问卷列表
     *
     * @return
     */
    @GetMapping
    public List<QuestionnaireVo> SoulQuestionnaire() {
        //获取问卷列表的出参
        List<QuestionnaireVo> questionnaireVos = this.testSoulService.getQuestionnaire();
        return questionnaireVos;
    }


    /**
     * 提交问卷
     *
     * @param answers 提交的答案们
    @PostMapping
    public void SubmitQuestionnaire(@RequestBody Map<String, List<Answers>> answers){
        List<Answers> answersList = answers.get("answers");
        this.testSoulService.SubmitQuestionnaire(answersList);
    }
*/

    @PostMapping
    public String SubmitQuestionnaire(@RequestBody String answers) {
        Map map = JSON.parseObject(answers);
        String answers1 = map.get("answers").toString();
        List<Answers> answersList = JSON.parseArray(answers1,Answers.class);
        return this.testSoulService.SubmitQuestionnaire(answersList);
    }

    /**
     * 查看结果
     * @param id
     * @return
     */
    @GetMapping("report/{id}")
    public ResponseEntity<UserReportVo> UserResult(@PathVariable("id") Long id){
        try {
            UserReportVo testResultVo = this.testSoulService.UserResult(id);
            return ResponseEntity.ok(testResultVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
